# Problem 4
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91  99.
# Find the largest palindrome made from the product of two 3-digit numbers.


import unittest

def isPalindrome(N):
    str = "%s" % N
    head = 0
    tail = len(str) - 1
    while head < tail:
        if str[head] != str[tail]:
            break
        head += 1
        tail -= 1
    if head < tail:
        return False
    return True

    
def findLargestPalindrome(xdigit):
    str = "1"
    xdigit -= 1
    for i in range(0, xdigit):
        str += '0'
    start = int(str)
    end = start * 10
    end -= 1
    start -= 1
    largest = 0
    for j in range(end, start, -1):
        for i in range(end, start, -1):
            n = j * i
            if isPalindrome(n):
                if n > largest:
                    largest = n
    return largest
        
    

  
class TestP004(unittest.TestCase):
    def xtest_isPalindromicNumber(self):
        self.assertTrue(isPalindrome(1))
        self.assertTrue(isPalindrome(8))
        self.assertFalse(isPalindrome(12))
        self.assertTrue(isPalindrome(121))
        self.assertTrue(isPalindrome(1221))
    
    def test_findLargestPalindrome(self):
        #self.assertEqual(findLargestPalindrome(1), 9)
        #self.assertEqual(findLargestPalindrome(2), 9009)
        self.assertEqual(findLargestPalindrome(3), 906609)


if __name__ == '__main__':
    P004 = unittest.TestLoader().loadTestsFromTestCase(TestP004)
    unittest.TextTestRunner(verbosity=2).run(P004)
    #print "The largest palindrome made from the product of two 3-digit numbers is [%d]" % findLargestPalindrome(3)